home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / admin / pwdutils.00 / pwdutils / pwdutils-1.00 / pwd_yp.c < prev    next >
C/C++ Source or Header  |  1996-05-16  |  4KB  |  164 lines

  1. /*
  2. ** Copyright 1996 Thorsten Kukuk <kukuk@uni-paderborn.de>
  3. **
  4. ** This program is free software; you can redistribute it and/or modify
  5. ** it under the terms of the GNU General Public License as published by
  6. ** the Free Software Foundation; either version 2 of the License, or
  7. ** (at your option) any later version.
  8. **
  9. ** This program is distributed in the hope that it will be useful,
  10. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. ** GNU General Public License for more details.
  13. **
  14. ** You should have received a copy of the GNU General Public License
  15. ** along with this program; if not, write to the Free Software
  16. ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18.  
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <netdb.h>
  23. #include <time.h>
  24. #include <sys/types.h>
  25. #include <pwd.h>
  26. #include <errno.h>
  27. #include <unistd.h>
  28. #include <limits.h>
  29. #include <rpc/rpc.h>
  30. #include <rpcsvc/yp_prot.h>
  31. #include <rpcsvc/ypclnt.h>
  32. /*
  33. ** Some distributions have a "/usr/include/rpcsvc/yppasswd.h" from
  34. ** Olaf Kirch, DON'T use that !
  35. */
  36. #include "yppasswd.h"
  37.  
  38. #include "pwdutils.h"
  39.  
  40. char *get_master_server( void )
  41. {
  42.   char *domainname, *master;
  43.   int port, err;
  44.   
  45.   if ((err = yp_get_default_domain(&domainname)) != 0) 
  46.     {
  47.       fprintf(stderr, "ERROR: can't get local NIS domainname: %s\n",
  48.               yperr_string(err));
  49.       return NULL;
  50.     }
  51.   
  52.   if ((err = yp_master(domainname, "passwd.byname", &master)) != 0) 
  53.     {
  54.       fprintf(stderr, "ERROR: can't find the master NIS server: %s\n",
  55.               yperr_string(err));
  56.       return NULL;
  57.     }
  58.  
  59.   port = getrpcport(master, YPPASSWDPROG, YPPASSWDPROC_UPDATE, IPPROTO_UDP);
  60.   if(port >= IPPORT_RESERVED) 
  61.     {
  62.       fprintf(stderr, "ERROR: yppasswd daemon running on illegal port %d.\n",
  63.           port);
  64.       return NULL;
  65.     }
  66.   if(port==0) 
  67.     {
  68.       fprintf(stderr, "ERROR: yppasswdd not running on NIS master \"%s\"\n",master);
  69.       return NULL;
  70.     }
  71.  
  72.   return master;
  73. }
  74.  
  75. int yp_writeback(struct passwd *pwd, char *oldpassword, char *master)
  76. {
  77.   struct timeval timeout;
  78.   struct yppasswd yppasswd;
  79.   CLIENT *clnt;
  80.   int    err, status;
  81.  
  82.   /* 
  83.   ** Initialize password information 
  84.   ** We couldn't use memcpy, since the newpw struct != passwd struct 
  85.   */
  86.   yppasswd.newpw.pw_name = pwd->pw_name;
  87.   yppasswd.newpw.pw_passwd = pwd->pw_passwd;
  88.   yppasswd.newpw.pw_uid = pwd->pw_uid;
  89.   yppasswd.newpw.pw_gid = pwd->pw_gid;
  90.   yppasswd.newpw.pw_gecos = pwd->pw_gecos;
  91.   yppasswd.newpw.pw_dir = pwd->pw_dir;
  92.   yppasswd.newpw.pw_shell = pwd->pw_shell;
  93.   yppasswd.oldpass = oldpassword;
  94.  
  95. #ifdef DEBUG
  96.   printf("name.....: [%s]\n",yppasswd.newpw.pw_name);
  97.   printf("password.: [%s]\n",yppasswd.newpw.pw_passwd);
  98.   printf("user id..: [%d]\n",yppasswd.newpw.pw_uid);
  99.   printf("group id.: [%d]\n",yppasswd.newpw.pw_gid);
  100.   printf("gecos....: [%s]\n",yppasswd.newpw.pw_gecos);
  101.   printf("directory: [%s]\n",yppasswd.newpw.pw_dir);
  102.   printf("shell....: [%s]\n",yppasswd.newpw.pw_shell);
  103. #endif
  104.   
  105.   clnt = clnt_create( master, YPPASSWDPROG, YPPASSWDVERS, "udp" );
  106.   clnt->cl_auth = authunix_create_default();
  107.   memset( (char*)&status, 0, sizeof(status) );
  108.   timeout.tv_sec = 25; timeout.tv_usec = 0;
  109.   err = clnt_call( clnt, YPPASSWDPROC_UPDATE,
  110.            (xdrproc_t) xdr_yppasswd, (char*)&yppasswd,
  111.            (xdrproc_t) xdr_int,(char*)&status,
  112.            timeout );
  113.   
  114.   if (err) 
  115.     {
  116.       clnt_perrno(err);
  117.       fprintf( stderr, "\n" );
  118.     } 
  119.  
  120.   auth_destroy( clnt->cl_auth );
  121.   clnt_destroy( clnt );
  122.   return((err || status) != 0);
  123. }
  124.  
  125. /*
  126. ** The following is generated by rpcgen and edit a little by me (TK)
  127. */
  128. bool_t xdr_x_passwd(XDR *xdrs, x_passwd *objp)
  129. {
  130.   if (!xdr_string(xdrs, &objp->pw_name, ~0)) 
  131.       return (FALSE);
  132.  
  133.   if (!xdr_string(xdrs, &objp->pw_passwd, ~0))
  134.     return (FALSE);
  135.          
  136.   if (!xdr_int(xdrs, &objp->pw_uid))
  137.     return (FALSE);
  138.   
  139.   if (!xdr_int(xdrs, &objp->pw_gid))
  140.     return (FALSE);
  141.   
  142.   if (!xdr_string(xdrs, &objp->pw_gecos, ~0))
  143.     return (FALSE);
  144.  
  145.   if (!xdr_string(xdrs, &objp->pw_dir, ~0))
  146.     return (FALSE);
  147.   
  148.   if (!xdr_string(xdrs, &objp->pw_shell, ~0))
  149.     return (FALSE);
  150.  
  151.   return (TRUE);
  152. }
  153.  
  154. bool_t xdr_yppasswd(XDR *xdrs, yppasswd *objp)
  155. {
  156.   if (!xdr_string(xdrs, &objp->oldpass, ~0))
  157.     return (FALSE);
  158.         
  159.   if (!xdr_x_passwd(xdrs, &objp->newpw))
  160.     return (FALSE);
  161.   
  162.   return (TRUE);
  163. }
  164.